package com.util;



import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.model.auto.StuScheme;
import com.model.auto.StuSchemeV2;
import com.model.auto.Student;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * Created by warden on 18/1/30.
 */
public class PdfUtil {


    public static void main(String[] args) throws Exception {
        List<StuScheme> list = new ArrayList<>();

        for(int i=0;i<5;i++){
            StuScheme stuSchemeResp = new StuScheme();
            stuSchemeResp.setId(i);
            stuSchemeResp.setProgrammeName("理工本科一批1");
            stuSchemeResp.setBatch("本科一批");
            stuSchemeResp.setSchemeName("志愿12L");
            stuSchemeResp.setSchemeNum(1);
            stuSchemeResp.setSchoolName("陕西理工大学");
            stuSchemeResp.setRecruitType("普通志愿");
            if(i==2){
                stuSchemeResp.setSchoolName("北京师范大学香港漫会大学联合国际学院");
                stuSchemeResp.setRecruitType("国家专项");
            }else if(i==3){
                stuSchemeResp.setSchoolName("陕西科技大学");
                stuSchemeResp.setRecruitType("");
            }else if(i==4){
                stuSchemeResp.setSchoolName("西北工业大学西北工业大学");
                stuSchemeResp.setBatch("提前批A段");
            }
            stuSchemeResp.setPlan2022(888);
            stuSchemeResp.setSchoolCode("0011");
            stuSchemeResp.setMajorPlan("一分数优先");
            stuSchemeResp.setSchemeChange("1");
            if(i==2){
                stuSchemeResp.setMajorList("[{\"sort\":\"专业6\",\"majorName\":\"计算机类| 计算机科学与技术1A++、软件工程2A++、计算机科学与技术(计算机科学实验班)1A++、计算机科学与技术(智班)1A++、计算机科学与技术(量子信息班)\",\"majorCode\":\"01\",\"plan2018\":123123,\"majorLimit\":\"5500\"},{\"sort\":\"专业2\",\"majorName\":\"计算机类| 计算机科学与技术1A++、软件工程2A++、计算机科学与技术(计算机科学实验班)1A++、计算机科学与技术(智班)1A++、计算机科学与技术(量子信息班)\",\"majorCode\":\"0C\",\"plan2018\":null,\"majorLimit\":\"5500\"},{\"sort\":\"专业3\",\"majorName\":\"理论与应用力学\",\"majorCode\":\"08\",\"plan2018\":null,\"majorLimit\":\"5500\"},{\"sort\":\"专业4\",\"majorName\":\"计算机科学与技术\",\"majorCode\":\"0D\",\"plan2018\":null,\"majorLimit\":\"5500\"},{\"sort\":\"专业5\",\"majorName\":\"物理学\",\"majorCode\":\"02\",\"plan2018\":null,\"majorLimit\":\"5500\"},{\"sort\":\"专业6\",\"majorName\":\"电子信息工程\",\"majorCode\":\"0B\",\"plan2018\":null,\"majorLimit\":\"5500\"},{\"sort\":\"备选\",\"majorName\":\"材料科学与工程\",\"majorCode\":\"09\",\"plan2018\":null,\"majorLimit\":\"5500\"},{\"sort\":\"专业6\",\"majorName\":\"网络空间安全\",\"majorCode\":\"0E\",\"plan2018\":null,\"majorLimit\":\"5500\"},{\"sort\":\"专业5\",\"majorName\":\"生物科学\",\"majorCode\":\"06\",\"plan2018\":null,\"majorLimit\":\"5500\"}]");
            }else if(i==3){
                stuSchemeResp.setMajorList("[]");
            }else{
                stuSchemeResp.setMajorList("[{\"sort\":\"专业6\",\"majorName\":\"数学与应用数学\",\"majorCode\":\"01\",\"plan2018\":1111,\"majorLimit\":\"5500\"},{\"sort\":\"专业2\",\"majorName\":\"人工智能\",\"majorCode\":\"0C\",\"plan2018\":null,\"majorLimit\":\"5500\"},{\"sort\":\"专业3\",\"majorName\":\"理论与应用力学\",\"majorCode\":\"08\",\"plan2018\":null,\"majorLimit\":\"5500\"},{\"sort\":\"专业4\",\"majorName\":\"计算机科学与技术\",\"majorCode\":\"0D\",\"plan2018\":null,\"majorLimit\":\"5500\"},{\"sort\":\"专业5\",\"majorName\":\"物理学\",\"majorCode\":\"02\",\"plan2018\":null,\"majorLimit\":\"5500\"},{\"sort\":\"专业6\",\"majorName\":\"电子信息工程\",\"majorCode\":\"0B\",\"plan2018\":null,\"majorLimit\":\"5500\"},{\"sort\":\"备选\",\"majorName\":\"材料科学与工程\",\"majorCode\":\"09\",\"plan2018\":null,\"majorLimit\":\"5500\"},{\"sort\":\"专业6\",\"majorName\":\"网络空间安全\",\"majorCode\":\"0E\",\"plan2018\":null,\"majorLimit\":\"5500\"},{\"sort\":\"专业5\",\"majorName\":\"生物科学\",\"majorCode\":\"06\",\"plan2018\":null,\"majorLimit\":\"5500\"}]");
            }


            list.add(stuSchemeResp);
        }

        Student student = new Student();
        student.setName("张三");
        student.setMajor(1);
        student.setScore("100");
        student.setPrecedence("10");
        String pdfPath = "D:\\1111\\test4.pdf";
        List<List<StuScheme>> scheme = new ArrayList<>();
        scheme.add(list);
        createPdf(pdfPath, scheme, student, true);

    }

    /**
     *
     * @param pdfPath
     * @param scheme
     * @param student
     * @param isAll
     * @return
     */
    public static String createPdf(String pdfPath, List<List<StuScheme>> scheme, Student student, Boolean isAll){
        String returnPath = pdfPath;
        try{
            //创建文件
            Document document = new Document();
            //建立一个书写器
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
            //打开文件
            document.open();
            int count = 0;
            for(List<StuScheme> list:scheme){
                if(count > 0){
                    document.add(new Paragraph(" ", getTitleChineseFont()));
                    document.add(new Paragraph(" ", getTitleChineseFont()));
                }
                //添加内容
                Paragraph title = new Paragraph(list.get(0).getProgrammeName(), getTitleChineseFont());
                title.setAlignment(Element.ALIGN_CENTER);


                Paragraph title1 = new Paragraph("  姓名："+student.getName(), getPdfChineseFont());
                if(student.getMajor() == 1){
                    title1.add("      科类：理科");
                }else if(student.getMajor() == 2){
                    title1.add("      科类：文史");
                }else if(student.getMajor() == 3){
                    title1.add("      科类：艺术理");
                }else if(student.getMajor() == 4){
                    title1.add("      科类：艺术文");
                }
                title1.add("      批次："+list.get(0).getBatch());
                if(StringUtils.hasText(student.getScore())){
                    title1.add("      分数："+student.getScore());
                }else{
                    title1.add("      分数：   ");
                }
                if(ObjectUtils.isEmpty(student.getPrecedence())){
                    title1.add("      位次：    ");
                }else{
                    title1.add("      位次："+student.getPrecedence());
                }

                title1.setAlignment(Element.ALIGN_CENTER);

                if(isAll && count == 0){
                    Paragraph allTitle = new Paragraph(student.getName()+"       高考志愿方案", getAllTitleChineseFont());
                    allTitle.setAlignment(Element.ALIGN_CENTER);
                    document.add(allTitle);
                    document.add(new Paragraph(" ", getTitleChineseFont()));
                    document.add(new Paragraph(" ", getTitleChineseFont()));
                }
                document.add(title);
                document.add(new Paragraph(" ", getTitleChineseFont()));
                document.add(title1);
                document.add(new Paragraph(" ", getPdfChineseFont()));

                float[] widths = {1.2f,1f,12f,1f,1.8f};
                PdfPTable table = new PdfPTable(widths);
                table.setPaddingTop(4f);
                table.setWidthPercentage(100); // 宽度100%填充
                table.setHorizontalAlignment(Element.ALIGN_LEFT);


                for(int i=0;i<5;i++){
                    PdfPCell firstCell = new PdfPCell();
                    if(i == 0){
                        Paragraph content = new Paragraph("序列",getPdfChineseFont());
                        firstCell.setPhrase(content);
                        firstCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        firstCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        firstCell.setMinimumHeight(18);//设置表格行高
                        table.addCell(firstCell);
                    }else if(i == 1){
                        Paragraph content = new Paragraph("代码",getPdfChineseFont());
                        firstCell.setPhrase(content);
                        firstCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        firstCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        firstCell.setMinimumHeight(18);//设置表格行高
                        table.addCell(firstCell);
                    }else if(i == 2){
                        Paragraph content = new Paragraph("院校名称(录取形式)/专业(类)|包含专业及其等级",getPdfChineseFont());
                        if(StringUtils.hasText(list.get(0).getRecruitType())){
                            content = new Paragraph("招生类别/院校名称(录取形式)/专业(类)|包含专业及其等级",getPdfChineseFont());
                        }
                        firstCell.setPhrase(content);
                        firstCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        firstCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        firstCell.setMinimumHeight(18);//设置表格行高
                        table.addCell(firstCell);
                    }else if(i == 3){
                        Paragraph content = new Paragraph("计划",getPdfChineseFont());
                        firstCell.setPhrase(content);
                        firstCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        firstCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        firstCell.setMinimumHeight(18);//设置表格行高
                        table.addCell(firstCell);
                    }else if(i == 4){
                        Paragraph content = new Paragraph("学费(元/年)",getPdfChineseFont());
                        firstCell.setPhrase(content);
                        firstCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        firstCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        firstCell.setMinimumHeight(18);//设置表格行高
                        table.addCell(firstCell);
                    }
                }


                if(list != null && list.size() >0){
                    for(StuScheme stuScheme:list){
                        if(!StringUtils.hasText(stuScheme.getSchemeName())){
                            continue;
                        }
                        int column = 1;
                        if(StringUtils.hasText(stuScheme.getMajorList())){
                            JSONArray stuMajorList = JSON.parseArray(stuScheme.getMajorList());
                            if(stuMajorList.size()>0){
                                column = stuMajorList.size();
                            }
                        }

                        PdfPCell pdfCell = new PdfPCell();
                        pdfCell.setMinimumHeight(20);//设置表格行高
                        String SchemeName = "";
                        if(stuScheme.getSchemeNum() != 7){
                            SchemeName = stuScheme.getSchemeName();
                        }else{
                            SchemeName = "备选";
                        }
                        Paragraph paragraph = new Paragraph(SchemeName, getPdfChineseBoldFont());
                        pdfCell.setRowspan(column);
                        pdfCell.setPhrase(paragraph);
                        pdfCell.setUseAscender(true);
                        pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        pdfCell.setBackgroundColor(new BaseColor(212, 212, 212));


                        PdfPCell pdfCell1 = new PdfPCell();
                        pdfCell1.setMinimumHeight(18);//设置表格行高
                        Paragraph paragraph1 = new Paragraph(stuScheme.getSchoolCode(), getPdfChineseBoldFont());
                        pdfCell1.setRowspan(column);
                        pdfCell1.setPhrase(paragraph1);
                        pdfCell1.setUseAscender(true);
                        pdfCell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        pdfCell1.setHorizontalAlignment(Element.ALIGN_CENTER);
                        pdfCell1.setBackgroundColor(new BaseColor(212, 212, 212));

                        PdfPCell pdfCell2 = new PdfPCell();
                        pdfCell2.setMinimumHeight(18);//设置表格行高
                        String schoolName = stuScheme.getSchoolName();
                        String majorPlan = "";

                        List<String> batch = Arrays.asList("本科一批,本科二批,国家专项,地方专项,高职专科".split(","));
                        if(batch.contains(stuScheme.getBatch())){
                            int maxLength = 34-stuScheme.getSchoolName().length();
                            if(StringUtils.hasText(stuScheme.getMajorPlan())){
                                maxLength = 38-stuScheme.getMajorPlan().length()-stuScheme.getSchoolName().length();
                            }
                            if(StringUtils.hasText(stuScheme.getRecruitType())){
                                maxLength = 37-stuScheme.getSchoolName().length()-stuScheme.getRecruitType().length();
                            }
                            if(StringUtils.hasText(stuScheme.getMajorPlan()) && StringUtils.hasText(stuScheme.getRecruitType())){
                                maxLength = 37-stuScheme.getMajorPlan().length()-stuScheme.getSchoolName().length()-stuScheme.getRecruitType().length();
                            }
                            for(int max=0;max<maxLength;max++){
                                majorPlan += "  ";
                            }
                            if(StringUtils.hasText(stuScheme.getMajorPlan())){
                                majorPlan += stuScheme.getMajorPlan()+"|";
                            }else{
                                majorPlan += " ";
                            }
                        }else{
                            int maxLength = 39-stuScheme.getSchoolName().length();
                            if(StringUtils.hasText(stuScheme.getRecruitType())){
                                maxLength = 38-stuScheme.getSchoolName().length()-stuScheme.getRecruitType().length();
                            }
                            for(int max=0;max<maxLength;max++){
                                majorPlan += "  ";
                            }
                        }

                        if("0".equals(stuScheme.getSchemeChange())){
                            majorPlan += "服从调剂：是";
                        }else{
                            majorPlan += "服从调剂：否";
                        }
                        Paragraph paragraph2 = new Paragraph();
                        if(StringUtils.hasText(stuScheme.getRecruitType())){
                            paragraph2.add(new Chunk(stuScheme.getRecruitType()+"   ", getPdfChineseBoldFont()));
                        }
                        paragraph2.add(new Chunk(schoolName, getPdfChineseBoldFont()));
                        paragraph2.add(new Chunk(majorPlan, getPdfChineseBoldFont()));
                        pdfCell2.setRowspan(column);
                        pdfCell2.setPhrase(paragraph2);
                        pdfCell2.setUseAscender(true);
                        pdfCell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        pdfCell2.setBackgroundColor(new BaseColor(212, 212, 212));

                        PdfPCell pdfCell3 = new PdfPCell();
                        pdfCell3.setMinimumHeight(18);//设置表格行高
                        String plan2022 = "";
                        if(stuScheme.getPlan2022() != null){
                            plan2022 = stuScheme.getPlan2022()+"";
                        }
                        Paragraph paragraph3 = new Paragraph(plan2022, getPdfChineseBoldFont());
                        pdfCell3.setRowspan(column);
                        pdfCell3.setPhrase(paragraph3);
                        pdfCell2.setUseAscender(true);
                        pdfCell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        pdfCell3.setHorizontalAlignment(Element.ALIGN_CENTER);
                        pdfCell3.setBackgroundColor(new BaseColor(212, 212, 212));

                        PdfPCell pdfCell4 = new PdfPCell();
                        pdfCell1.setMinimumHeight(18);//设置表格行高
                        Paragraph paragraph4 = new Paragraph("", getPdfChineseBoldFont());
                        pdfCell4.setRowspan(column);
                        pdfCell4.setPhrase(paragraph4);
                        pdfCell4.setUseAscender(true);
                        pdfCell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        pdfCell4.setHorizontalAlignment(Element.ALIGN_CENTER);
                        pdfCell4.setBackgroundColor(new BaseColor(212, 212, 212));

                        table.addCell(pdfCell);
                        table.addCell(pdfCell1);
                        table.addCell(pdfCell2);
                        table.addCell(pdfCell3);
                        table.addCell(pdfCell4);

                        if(StringUtils.hasText(stuScheme.getMajorList())){
                            JSONArray jsonArray = JSON.parseArray(stuScheme.getMajorList());
                            if(jsonArray.size()>0){
//                                jsonArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getString("sort")));
                                for(int i =0;i<jsonArray.size();i++){
                                    JSONObject major = jsonArray.getJSONObject(i);

                                    PdfPCell majorPdfCell = new PdfPCell();
                                    majorPdfCell.setMinimumHeight(20);//设置表格行高

                                    Paragraph majorParagraph = new Paragraph(major.getString("sort"), getPdfChineseFont());
                                    majorPdfCell.setRowspan(column);
                                    majorPdfCell.setPhrase(majorParagraph);
                                    majorPdfCell.setUseAscender(true);
                                    majorPdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                                    majorPdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);

                                    PdfPCell majorPdfCell1 = new PdfPCell();
                                    majorPdfCell1.setMinimumHeight(20);//设置表格行高

                                    Paragraph majorParagraph1 = new Paragraph(major.getString("majorCode"), getPdfChineseFont());
                                    majorPdfCell1.setRowspan(column);
                                    majorPdfCell1.setPhrase(majorParagraph1);
                                    majorPdfCell1.setUseAscender(true);
                                    majorPdfCell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
                                    majorPdfCell1.setHorizontalAlignment(Element.ALIGN_CENTER);

                                    PdfPCell majorPdfCell2 = new PdfPCell();
                                    majorPdfCell2.setMinimumHeight(20);//设置表格行高

                                    Paragraph majorParagraph2 = new Paragraph(major.getString("majorName"), getPdfChineseFont());
                                    majorPdfCell2.setRowspan(column);
                                    majorPdfCell2.setPhrase(majorParagraph2);
                                    majorPdfCell2.setUseAscender(true);
                                    majorPdfCell2.setVerticalAlignment(Element.ALIGN_MIDDLE);

                                    PdfPCell majorPdfCell3 = new PdfPCell();
                                    majorPdfCell3.setMinimumHeight(20);//设置表格行高

                                    Paragraph majorParagraph3 = new Paragraph(major.getString("plan2018"), getPdfChineseFont());
                                    majorPdfCell3.setRowspan(column);
                                    majorPdfCell3.setPhrase(majorParagraph3);
                                    majorPdfCell3.setUseAscender(true);
                                    majorPdfCell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
                                    majorPdfCell3.setHorizontalAlignment(Element.ALIGN_CENTER);

                                    PdfPCell majorPdfCell4 = new PdfPCell();
                                    majorPdfCell4.setMinimumHeight(20);//设置表格行高

                                    Paragraph majorParagraph4 = new Paragraph(major.getString("majorLimit"), getPdfChineseFont());
                                    majorPdfCell4.setRowspan(column);
                                    majorPdfCell4.setPhrase(majorParagraph4);
                                    majorPdfCell4.setUseAscender(true);
                                    majorPdfCell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
                                    majorPdfCell4.setHorizontalAlignment(Element.ALIGN_CENTER);


                                    table.addCell(majorPdfCell);
                                    table.addCell(majorPdfCell1);
                                    table.addCell(majorPdfCell2);
                                    table.addCell(majorPdfCell3);
                                    table.addCell(majorPdfCell4);
                                }
                            }
                        }

                    }
                }
                document.add(table);
                count++;
            }

            Paragraph remark = new Paragraph("\n\n\n重要声明\n", getRemarkChineseFont());
            remark.setAlignment(Element.ALIGN_CENTER);
            document.add(remark);

            Paragraph remark1 = new Paragraph("\n1、数据来源于各省教育考试院，由于各省市加分政策、学校官网对征集志愿数据的显示等原因，考试院数据会与院校官网数据存在不一致的情况，建议以教育考试院发布的数据为准。\n" +
                    "2、所有涉及学费、学制、招生代码等信息请最终以教育考试院下发的招生计划书为准。\n" +
                    "3、冲稳保推荐结果及录取概率是根据往年的录取数据分析得出，仅供参考。\n" +
                    "4、本系统不等同于官方实际的网上填报志愿，正式填报请登录省教育考试院官网。考虑到志愿填报存在诸多不确定因素，所有数据仅供参考，不作为您填报志愿的绝对依据，使用过程中如有问题，请咨询客服。\n",
                    getRemark1ChineseFont());
            document.add(remark1);

            document.close();
            //关闭文档
            document.close();
            //关闭书写器
            writer.close();

            returnPath = createBeginImage(pdfPath);
        }catch (Exception e){
            e.printStackTrace();
        }

        return returnPath;
    }

    /**
     *
     * @param pdfPath
     * @param scheme
     * @param student
     * @param isAll
     * @return
     */
    public static String createPdfV2(String pdfPath, List<List<StuSchemeV2>> scheme, Student student, Boolean isAll){
        String returnPath = pdfPath;
        try{
            //创建文件
            Document document = new Document();
            //建立一个书写器
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfPath));
            //打开文件
            document.open();
            int count = 0;
            for(List<StuSchemeV2> list:scheme){
                if(count > 0){
                    document.add(new Paragraph(" ", getTitleChineseFont()));
                    document.add(new Paragraph(" ", getTitleChineseFont()));
                }
                //添加内容
                Paragraph title = new Paragraph(list.get(0).getProgrammeName(), getTitleChineseFont());
                title.setAlignment(Element.ALIGN_CENTER);


                Paragraph title1 = new Paragraph("  姓名："+student.getName(), getPdfChineseFont());
                if(student.getMajor() == 1){
                    title1.add("      科类：物理");
                }else if(student.getMajor() == 2){
                    title1.add("      科类：历史");
                }else if(student.getMajor() == 3){
                    title1.add("      科类：物理");
                }else if(student.getMajor() == 4){
                    title1.add("      科类：历史");
                }
                if(StringUtils.hasText(student.getSubject1())){
                    title1.add("  "+student.getSubject1());
                }
                if(StringUtils.hasText(student.getSubject2())){
                    title1.add("  "+student.getSubject2());
                }
                title1.add("      批次："+list.get(0).getBatch());
                if(StringUtils.hasText(student.getScore())){
                    title1.add("      分数："+student.getScore());
                }else{
                    title1.add("      分数：   ");
                }
                if(ObjectUtils.isEmpty(student.getPrecedence())){
                    title1.add("      位次：    ");
                }else{
                    title1.add("      位次："+student.getPrecedence());
                }

                title1.setAlignment(Element.ALIGN_CENTER);

                if(isAll && count == 0){
                    Paragraph allTitle = new Paragraph(student.getName()+"       高考志愿方案", getAllTitleChineseFont());
                    allTitle.setAlignment(Element.ALIGN_CENTER);
                    document.add(allTitle);
                    document.add(new Paragraph(" ", getTitleChineseFont()));
                    document.add(new Paragraph(" ", getTitleChineseFont()));
                }
                document.add(title);
                document.add(new Paragraph(" ", getTitleChineseFont()));
                document.add(title1);
                document.add(new Paragraph(" ", getPdfChineseFont()));

                float[] widths = {1f,2f,10f,1.8f,2.6f,1f,2f};
                PdfPTable table = new PdfPTable(widths);
                table.setPaddingTop(4f);
                table.setWidthPercentage(100); // 宽度100%填充
                table.setHorizontalAlignment(Element.ALIGN_LEFT);


                for(int i=0;i<7;i++){
                    PdfPCell firstCell = new PdfPCell();
                    if(i == 0){
                        Paragraph content = new Paragraph("序列",getPdfChineseFont());
                        firstCell.setPhrase(content);
                        firstCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        firstCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        firstCell.setMinimumHeight(18);//设置表格行高
                        table.addCell(firstCell);
                    }else if(i == 1){
                        Paragraph content = new Paragraph("代码",getPdfChineseFont());
                        firstCell.setPhrase(content);
                        firstCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        firstCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        firstCell.setMinimumHeight(18);//设置表格行高
                        table.addCell(firstCell);
                    }else if(i == 2){
                        Paragraph content = new Paragraph("院校名称(录取形式)/专业(类)|包含专业及其等级",getPdfChineseFont());
                        firstCell.setPhrase(content);
                        firstCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        firstCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        firstCell.setMinimumHeight(18);//设置表格行高
                        table.addCell(firstCell);
                    }else if(i == 3){
                        Paragraph content = new Paragraph("招生类型",getPdfChineseFont());
                        firstCell.setPhrase(content);
                        firstCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        firstCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        firstCell.setMinimumHeight(18);//设置表格行高
                        table.addCell(firstCell);
                    }else if(i == 4){
                        Paragraph content = new Paragraph("投档线/投档位次",getPdfChineseFont());
                        firstCell.setPhrase(content);
                        firstCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        firstCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        firstCell.setMinimumHeight(18);//设置表格行高
                        table.addCell(firstCell);
                    }else if(i == 5){
                        Paragraph content = new Paragraph("计划",getPdfChineseFont());
                        firstCell.setPhrase(content);
                        firstCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        firstCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        firstCell.setMinimumHeight(18);//设置表格行高
                        table.addCell(firstCell);
                    }else if(i == 6){
                        Paragraph content = new Paragraph("学费(元/年)",getPdfChineseFont());
                        firstCell.setPhrase(content);
                        firstCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        firstCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        firstCell.setMinimumHeight(18);//设置表格行高
                        table.addCell(firstCell);
                    }
                }


                if(list != null && list.size() >0){
                    for(StuSchemeV2 stuScheme:list){
                        if(!StringUtils.hasText(stuScheme.getSchemeName())){
                            continue;
                        }
                        int column = 1;
                        if(StringUtils.hasText(stuScheme.getMajorList())){
                            JSONArray stuMajorList = JSON.parseArray(stuScheme.getMajorList());
                            if(stuMajorList.size()>0){
                                column = stuMajorList.size();
                            }
                        }

                        PdfPCell pdfCell = new PdfPCell();
                        pdfCell.setMinimumHeight(20);//设置表格行高
                        String schemeName = "志愿"+stuScheme.getSchemeName();
                        Paragraph paragraph = new Paragraph(schemeName, getPdfChineseBoldFont());
                        pdfCell.setRowspan(column);
                        pdfCell.setPhrase(paragraph);
                        pdfCell.setUseAscender(true);
                        pdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        pdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);
                        pdfCell.setBackgroundColor(new BaseColor(212, 212, 212));


                        PdfPCell pdfCell1 = new PdfPCell();
                        pdfCell1.setMinimumHeight(18);//设置表格行高
                        Paragraph paragraph1 = new Paragraph(stuScheme.getSchoolCode()+"-"+stuScheme.getGroupCode(), getPdfChineseBoldFont());
                        pdfCell1.setRowspan(column);
                        pdfCell1.setPhrase(paragraph1);
                        pdfCell1.setUseAscender(true);
                        pdfCell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        pdfCell1.setHorizontalAlignment(Element.ALIGN_CENTER);
                        pdfCell1.setBackgroundColor(new BaseColor(212, 212, 212));

                        PdfPCell pdfCell2 = new PdfPCell();
                        pdfCell2.setMinimumHeight(18);//设置表格行高
                        String schoolName = stuScheme.getSchoolName();
                        String majorPlan = "";

                        List<String> batch = Arrays.asList("本科一批,本科二批,国家专项,地方专项,高职专科".split(","));
                        if(batch.contains(stuScheme.getBatch())){
                            int maxLength = 33-stuScheme.getSchoolName().length();
                            if(StringUtils.hasText(stuScheme.getMajorPlan())){
                                maxLength = 37-stuScheme.getMajorPlan().length()-stuScheme.getSchoolName().length();
                            }
                            for(int max=0;max<maxLength;max++){
                                majorPlan += "  ";
                            }
                            majorPlan += stuScheme.getMajorPlan()+"|";
                        }else{
                            int maxLength = 25-stuScheme.getSchoolName().length();
                            for(int max=0;max<maxLength;max++){
                                majorPlan += "  ";
                            }
                        }

                        if("0".equals(stuScheme.getSchemeChange())){
                            majorPlan += "服从调剂：是";
                        }else{
                            majorPlan += "服从调剂：否";
                        }
                        Paragraph paragraph2 = new Paragraph();
                        paragraph2.add(new Chunk(schoolName, getPdfChineseBoldFont()));
                        paragraph2.add(new Chunk(majorPlan, getPdfChineseBoldFont()));
                        pdfCell2.setRowspan(column);
                        pdfCell2.setPhrase(paragraph2);
                        pdfCell2.setUseAscender(true);
                        pdfCell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        pdfCell2.setBackgroundColor(new BaseColor(212, 212, 212));

                        PdfPCell pdfCell3 = new PdfPCell();
                        pdfCell3.setMinimumHeight(18);//设置表格行高
                        Paragraph paragraph3 = new Paragraph(stuScheme.getRecruitType(), getPdfChineseBoldFont());
                        pdfCell3.setRowspan(column);
                        pdfCell3.setPhrase(paragraph3);
                        pdfCell3.setUseAscender(true);
                        pdfCell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        pdfCell3.setHorizontalAlignment(Element.ALIGN_CENTER);
                        pdfCell3.setBackgroundColor(new BaseColor(212, 212, 212));

                        PdfPCell pdfCell4 = new PdfPCell();
                        pdfCell4.setMinimumHeight(18);//设置表格行高
                        Paragraph paragraph4 = new Paragraph(stuScheme.getShiftLine2021()+"/"+stuScheme.getRanking2021(), getPdfChineseBoldFont());
                        pdfCell4.setRowspan(column);
                        pdfCell4.setPhrase(paragraph4);
                        pdfCell4.setUseAscender(true);
                        pdfCell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        pdfCell4.setHorizontalAlignment(Element.ALIGN_CENTER);
                        pdfCell4.setBackgroundColor(new BaseColor(212, 212, 212));

                        PdfPCell pdfCell5 = new PdfPCell();
                        pdfCell5.setMinimumHeight(18);//设置表格行高
                        String plan2022 = "";
                        if(stuScheme.getPlan2022() != null){
                            plan2022 = stuScheme.getPlan2022()+"";
                        }
                        Paragraph paragraph5 = new Paragraph(plan2022, getPdfChineseBoldFont());
                        pdfCell5.setRowspan(column);
                        pdfCell5.setPhrase(paragraph5);
                        pdfCell2.setUseAscender(true);
                        pdfCell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        pdfCell5.setHorizontalAlignment(Element.ALIGN_CENTER);
                        pdfCell5.setBackgroundColor(new BaseColor(212, 212, 212));

                        PdfPCell pdfCell6 = new PdfPCell();
                        pdfCell1.setMinimumHeight(18);//设置表格行高
                        Paragraph paragraph6 = new Paragraph("", getPdfChineseBoldFont());
                        pdfCell6.setRowspan(column);
                        pdfCell6.setPhrase(paragraph6);
                        pdfCell6.setUseAscender(true);
                        pdfCell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
                        pdfCell6.setHorizontalAlignment(Element.ALIGN_CENTER);
                        pdfCell6.setBackgroundColor(new BaseColor(212, 212, 212));

                        table.addCell(pdfCell);
                        table.addCell(pdfCell1);
                        table.addCell(pdfCell2);
                        table.addCell(pdfCell3);
                        table.addCell(pdfCell4);
                        table.addCell(pdfCell5);
                        table.addCell(pdfCell6);

                        if(StringUtils.hasText(stuScheme.getMajorList())){
                            JSONArray jsonArray = JSON.parseArray(stuScheme.getMajorList());
                            if(jsonArray.size()>0){
//                                jsonArray.sort(Comparator.comparing(obj -> ((JSONObject) obj).getString("sort")));
                                for(int i =0;i<jsonArray.size();i++){
                                    JSONObject major = jsonArray.getJSONObject(i);

                                    PdfPCell majorPdfCell = new PdfPCell();
                                    majorPdfCell.setMinimumHeight(20);//设置表格行高

                                    Paragraph majorParagraph = new Paragraph(major.getString("sort"), getPdfChineseFont());
                                    majorPdfCell.setRowspan(column);
                                    majorPdfCell.setPhrase(majorParagraph);
                                    majorPdfCell.setUseAscender(true);
                                    majorPdfCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
                                    majorPdfCell.setHorizontalAlignment(Element.ALIGN_CENTER);

                                    PdfPCell majorPdfCell1 = new PdfPCell();
                                    majorPdfCell1.setMinimumHeight(20);//设置表格行高

                                    Paragraph majorParagraph1 = new Paragraph(major.getString("majorCode"), getPdfChineseFont());
                                    majorPdfCell1.setRowspan(column);
                                    majorPdfCell1.setPhrase(majorParagraph1);
                                    majorPdfCell1.setUseAscender(true);
                                    majorPdfCell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
                                    majorPdfCell1.setHorizontalAlignment(Element.ALIGN_CENTER);

                                    PdfPCell majorPdfCell2 = new PdfPCell();
                                    majorPdfCell2.setMinimumHeight(20);//设置表格行高
                                    String majorName = major.getString("majorName");
                                    if(StringUtils.hasText(major.getString("level"))){
                                        majorName += "【"+major.getString("level")+"】";
                                    }
                                    Paragraph majorParagraph2 = new Paragraph(majorName, getPdfChineseFont());
                                    majorPdfCell2.setRowspan(column);
                                    majorPdfCell2.setPhrase(majorParagraph2);
                                    majorPdfCell2.setUseAscender(true);
                                    majorPdfCell2.setVerticalAlignment(Element.ALIGN_MIDDLE);

                                    PdfPCell majorPdfCell3 = new PdfPCell();
                                    majorPdfCell3.setMinimumHeight(20);//设置表格行高

                                    Paragraph majorParagraph3 = new Paragraph("", getPdfChineseFont());
                                    majorPdfCell3.setRowspan(column);
                                    majorPdfCell3.setPhrase(majorParagraph3);
                                    majorPdfCell3.setUseAscender(true);
                                    majorPdfCell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
                                    majorPdfCell3.setHorizontalAlignment(Element.ALIGN_CENTER);

                                    PdfPCell majorPdfCell4 = new PdfPCell();
                                    majorPdfCell4.setMinimumHeight(20);//设置表格行高
                                    String majorParagraph4Str = "";
                                    if(StringUtils.hasText(major.getString("minShiftLine2017"))){
                                        majorParagraph4Str+=major.getString("minShiftLine2017");
                                    }
                                    if(StringUtils.hasText(major.getString("minRanking2017"))){
                                        majorParagraph4Str+="/"+major.getString("minRanking2017");
                                    }
                                    Paragraph majorParagraph4 = new Paragraph(majorParagraph4Str, getPdfChineseFont());
                                    majorPdfCell4.setRowspan(column);
                                    majorPdfCell4.setPhrase(majorParagraph4);
                                    majorPdfCell4.setUseAscender(true);
                                    majorPdfCell4.setVerticalAlignment(Element.ALIGN_MIDDLE);
                                    majorPdfCell4.setHorizontalAlignment(Element.ALIGN_CENTER);

                                    PdfPCell majorPdfCell5 = new PdfPCell();
                                    majorPdfCell5.setMinimumHeight(20);//设置表格行高

                                    Paragraph majorParagraph5 = new Paragraph(major.getString("plan2018"), getPdfChineseFont());
                                    majorPdfCell5.setRowspan(column);
                                    majorPdfCell5.setPhrase(majorParagraph5);
                                    majorPdfCell5.setUseAscender(true);
                                    majorPdfCell5.setVerticalAlignment(Element.ALIGN_MIDDLE);
                                    majorPdfCell5.setHorizontalAlignment(Element.ALIGN_CENTER);

                                    PdfPCell majorPdfCell6 = new PdfPCell();
                                    majorPdfCell6.setMinimumHeight(20);//设置表格行高

                                    Paragraph majorParagraph6 = new Paragraph(major.getString("majorLimit"), getPdfChineseFont());
                                    majorPdfCell6.setRowspan(column);
                                    majorPdfCell6.setPhrase(majorParagraph6);
                                    majorPdfCell6.setUseAscender(true);
                                    majorPdfCell6.setVerticalAlignment(Element.ALIGN_MIDDLE);
                                    majorPdfCell6.setHorizontalAlignment(Element.ALIGN_CENTER);


                                    table.addCell(majorPdfCell);
                                    table.addCell(majorPdfCell1);
                                    table.addCell(majorPdfCell2);
                                    table.addCell(majorPdfCell3);
                                    table.addCell(majorPdfCell4);
                                    table.addCell(majorPdfCell5);
                                    table.addCell(majorPdfCell6);
                                }
                            }
                        }

                    }
                }
                document.add(table);
                count++;
            }

            Paragraph remark = new Paragraph("\n\n\n重要声明\n", getRemarkChineseFont());
            remark.setAlignment(Element.ALIGN_CENTER);
            document.add(remark);

            Paragraph remark1 = new Paragraph("\n1、数据来源于各省教育考试院，由于各省市加分政策、学校官网对征集志愿数据的显示等原因，考试院数据会与院校官网数据存在不一致的情况，建议以教育考试院发布的数据为准。\n" +
                    "2、所有涉及学费、学制、招生代码等信息请最终以教育考试院下发的招生计划书为准。\n" +
                    "3、冲稳保推荐结果及录取概率是根据往年的录取数据分析得出，仅供参考。\n" +
                    "4、本系统不等同于官方实际的网上填报志愿，正式填报请登录省教育考试院官网。考虑到志愿填报存在诸多不确定因素，所有数据仅供参考，不作为您填报志愿的绝对依据，使用过程中如有问题，请咨询客服。\n",
                    getRemark1ChineseFont());
            document.add(remark1);

            document.close();
            //关闭文档
            document.close();
            //关闭书写器
            writer.close();

            returnPath = createBeginImage(pdfPath);
        }catch (Exception e){
            e.printStackTrace();
        }

        return returnPath;
    }

    public static String createBeginText(String content,String pdfPath,String exportPath) throws Exception {

        //图片水印
        PdfReader reader = new PdfReader(pdfPath);
        PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(exportPath));

        //文字水印
        PdfContentByte over = stamp.getOverContent(1);
        over.beginText();
        BaseFont bf = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        over.setFontAndSize(bf, 18);
        over.setTextMatrix(30, 30);
        over.showTextAligned(Element.ALIGN_LEFT, content, 230, 430, 45);
        over.endText();
        stamp.close();
        reader.close();
        return "";
    }

    public static String createBeginImage(String pdfPath) throws Exception {

        String exportPath = pdfPath.replace(".pdf","_1.pdf");

        //图片水印
        PdfReader reader = new PdfReader(pdfPath);
        PdfStamper stamp = new PdfStamper(reader, new FileOutputStream(exportPath));

        File file = ResourceUtils.getFile("classpath:image/beijing.png");
        URL url=file.toURL();
        Image img = Image.getInstance(url);
        img.setAbsolutePosition(130, 400);
        for(int i=1;i<20;i++){
            PdfContentByte under = stamp.getUnderContent(i);
            if(under == null){
                break;
            }
            under.addImage(img);
        }

        stamp.close();
        reader.close();
        return exportPath;
    }

    public static Font getPdfChineseFont() throws Exception {
//        BaseFont bfChinese = BaseFont.createFont("C:/Windows/Fonts/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        BaseFont bfChinese = BaseFont.createFont("/data/mendaoVoluntary/font/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        Font fontChinese = new Font(bfChinese, 8, Font.NORMAL);
        return fontChinese;
    }

    public static Font getPdfChineseBoldFont() throws Exception {
//        BaseFont bfChinese = BaseFont.createFont("C:/Windows/Fonts/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        BaseFont bfChinese = BaseFont.createFont("/data/mendaoVoluntary/font/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        Font fontChinese = new Font(bfChinese, 8, Font.BOLD);
        return fontChinese;
    }

    public static Font getTitleChineseFont() throws Exception {
//        BaseFont bfChinese = BaseFont.createFont("C:/Windows/Fonts/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        BaseFont bfChinese = BaseFont.createFont("/data/mendaoVoluntary/font/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        Font fontChinese = new Font(bfChinese, 12, Font.NORMAL);
        return fontChinese;
    }

    public static Font getAllTitleChineseFont() throws Exception {
        BaseFont bfChinese = BaseFont.createFont("C:/Windows/Fonts/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//        BaseFont bfChinese = BaseFont.createFont("/data/mendaoVoluntary/font/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        Font fontChinese = new Font(bfChinese, 16, Font.NORMAL);
        return fontChinese;
    }

    public static Font getRemarkChineseFont() throws Exception {
//        BaseFont bfChinese = BaseFont.createFont("C:/Windows/Fonts/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        BaseFont bfChinese = BaseFont.createFont("/data/mendaoVoluntary/font/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        Font fontChinese = new Font(bfChinese, 16, Font.NORMAL, BaseColor.RED);
        return fontChinese;
    }

    public static Font getRemark1ChineseFont() throws Exception {
//        BaseFont bfChinese = BaseFont.createFont("C:/Windows/Fonts/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        BaseFont bfChinese = BaseFont.createFont("/data/mendaoVoluntary/font/simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

        Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, BaseColor.RED);
        return fontChinese;
    }

}
